package cn.org.itool.ws.framework.protocol.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.org.itool.ws.framework.constent.WsProtocolEnum;
import cn.org.itool.ws.framework.netty.message.*;
import cn.org.itool.ws.framework.protocol.service.ProtocolService;
import cn.org.itool.ws.framework.utils.ObjectJsonUtil;
import cn.org.itool.ws.framework.ws.WsServer;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Log4j2
public class ProtocolBizServiceImpl extends ProtocolService {


    @Override
    public WsProtocolEnum type() {
        return WsProtocolEnum.BIZ;
    }

    @Override
    public List<BizWsPackage> decodePackage(Object text) {
        List<BizWsPackage> packageList = new ArrayList<>();
        BizWsPackage bizWsPackage = ObjectJsonUtil.fromJson(String.valueOf(text), BizWsPackage.class);
        packageList.add(bizWsPackage);
        return packageList;
    }

    @Override
    public BizWsMessage decodeMessage(Object message) {
        String msgStr;
        if(message instanceof Map) {
            msgStr = ObjectJsonUtil.toJson(message);
        } else {
            msgStr = String.valueOf(message);
        }
        BizWsMessage bizWsMessage = JSON.parseObject(msgStr, BizWsMessage.class);

//        ObjectJsonUtil.fromJson(String.valueOf(message), BizWsMessage.class);
        return bizWsMessage ;
    }

    @Override
    public Object encode(WsHandShake resp) {
        BizWsPackage bizWsPackage = new BizWsPackage();
        bizWsPackage.setType(BizWsPackage.HAND_SHAKE);

        JSONObject data = new JSONObject();
        data.put("code", 200);
        data.put("heartbeat", WsServer.NETTY_PROPERTIES.getHeartbeat());

        bizWsPackage.setBody(data);
        String text="";
        try {
            text = OBJECT_MAPPER.writeValueAsString(bizWsPackage);
        }catch (Exception e){
            log.error("e", e);
        }
        return new TextWebSocketFrame(text);
    }

    @Override
    public Object encode(WsHandShakeAck resp) {
        BizWsPackage bizWsPackage = new BizWsPackage();
        bizWsPackage.setType(BizWsPackage.HAND_SHAKE_AC);
        bizWsPackage.setBody(null);
        String text="";
        try {
            text = OBJECT_MAPPER.writeValueAsString(bizWsPackage);
        }catch (Exception e){
            log.error("e", e);
        }
        return new TextWebSocketFrame(text);
    }

    @Override
    public Object encode(WsHeartbeat resp) {
        BizWsPackage bizWsPackage = new BizWsPackage();
        bizWsPackage.setType(BizWsPackage.HEART_BEAT);
        bizWsPackage.setBody(null);
        String text="";
        try {
            text = OBJECT_MAPPER.writeValueAsString(bizWsPackage);
        }catch (Exception e){
            log.error("e", e);
        }
        return new TextWebSocketFrame(text);
    }

    @Override
    public Object encode(BizWsMessage resp) {
        try {
            BizWsPackage bizWsPackage = new BizWsPackage();
            bizWsPackage.setType(BizWsPackage.DATA);
            bizWsPackage.setBody(resp);
            String text = OBJECT_MAPPER.writeValueAsString(bizWsPackage);
            return new TextWebSocketFrame(text);
        }catch (Exception e) {
            log.error("e:", e);
        }
        return null;
    }
}
